package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.common.config.minio.MinioProperties;
import com.atguigu.gmall.product.service.FileUploadService;
import io.minio.MinioClient;
import io.minio.PutObjectOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.UUID;


@Service
public class FileUploadServiceImpl implements FileUploadService {

    @Autowired
    MinioClient client;

    @Autowired
    MinioProperties minioProperties;
    @Override
    public String upload(MultipartFile file) throws Exception {

        String filename = UUID.randomUUID().toString().replace("-","")+"_"+file.getOriginalFilename();
        InputStream fis = file.getInputStream();

        //3.上传文件
        PutObjectOptions options = new PutObjectOptions(file.getSize(),-1L);
        client.putObject(minioProperties.getBucketName(),filename,fis,options);

        //4.拿到文件的访问路径
        String url = minioProperties.getEndpoint()+"/"+minioProperties.getBucketName()+"/" + filename;

        return url;
    }
}
